package com.easygoods.controller;

import com.bestvike.stone.core.collection.CollectionUtils;
import com.bestvike.stone.core.lang.BeanUtils;
import com.bestvike.stone.core.lang.Convert;
import com.bestvike.stone.spring.controller.BaseController;
import com.bestvike.stone.spring.exception.AssertUtils;
import com.bestvike.stone.spring.rest.common.CommonResponse;
import com.bestvike.stone.spring.shiro.annotation.RequiresPermissions;
import com.easygoods.enums.AddressType;
import com.easygoods.request.AddressAddRequest;
import com.easygoods.request.AddressDeleteRequest;
import com.easygoods.request.AddressModifyRequest;
import com.easygoods.request.AddressSetDefaultRequest;
import com.easygoods.response.AddressListResponse;
import com.easygoods.service.AddressService;
import com.easygoods.shiro.auth.UserPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zhq
 * @date 2019/1/9 15:44
 */
@RestController
@RequestMapping("/api/address")
public class AddressController extends BaseController {
    @Autowired
    private AddressService addressService;

    //新增收货地址、寄件地址，根据地址类型来区分
    @RequiresPermissions(UserPermissions.BUY)
    @PostMapping("/add")
    public CommonResponse<?> addAddress(@RequestBody AddressAddRequest request) {
        this.addressService.addAddress(request);
        return CommonResponse.success();
    }

    //根据主键id删除收货地址、寄件地址
    @RequiresPermissions(UserPermissions.BUY)
    @PostMapping("/delete")
    public CommonResponse<?> removeAddress(@RequestBody AddressDeleteRequest request) {
        this.addressService.removeAddress(request);
        return CommonResponse.success();
    }

    //修改收货、寄件地址
    @RequiresPermissions(UserPermissions.BUY)
    @PostMapping("/modify")
    public CommonResponse<?> modifyAddress(@RequestBody AddressModifyRequest request) {
        this.addressService.modifyAddress(request);
        return CommonResponse.success();
    }

    //设为默认地址
    @RequiresPermissions(UserPermissions.BUY)
    @PostMapping("/default")
    public CommonResponse<?> setAsDefaultAddress(@RequestBody AddressSetDefaultRequest request) {
        this.addressService.setAsDefaultAddress(request);
        return CommonResponse.success();
    }

    //查询收货地址、寄件地址
    @RequiresPermissions(UserPermissions.BUY)
    @GetMapping("/getAddresses")
    public CommonResponse<AddressListResponse> getAddresses(String addressType) {
        AssertUtils.hasText(addressType, "addressType");
        AddressType addressType1 = Convert.defaultType(addressType, AddressType.class);
        AssertUtils.isTrue(addressType1 != null, "addressType can not be null");
        AddressListResponse addressListResponseItemList = this.addressService.getAddressList(addressType1);
        return CommonResponse.success(addressListResponseItemList);

    }
}
